package com.service.infoEntry.task;

import com.service.config.utils.DateUtil;
import com.service.config.utils.IdUtil;
import com.service.infoEntry.Rabbit.MsgProducer;
import com.service.infoEntry.config.MailBean;
import com.service.infoEntry.dao.UserInfoDao;
import com.service.infoEntry.entity.*;
import com.service.infoEntry.service.ProgrammeService;
import com.service.infoEntry.service.RouteInfoService;
import com.service.infoEntry.service.TeamService;
import com.service.infoEntry.service.TransactionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.service.config.utils.DateUtil.dateToWeek;
import static com.service.config.utils.DateUtil.initDateByDay;
import static com.service.config.utils.DateUtils.YYYY_MM_DD;
import static com.service.config.utils.DateUtils.dateTime;
import static com.service.infoEntry.constant.Constant.*;

/**
 * @program: CloudCampusService
 * @description: 定时任务
 * @author: rui
 * @create: 2021-03-30 15:35
 **/
@Component
@Async
@Slf4j
public class ScheduleService {
    @Value("${Spring.mail.username}")
    private String MAIL_SENDER; //邮件发送者
    @Value("${Spring.mail.password}")
    private String password; //邮件发送者
    @Value("${Spring.mail.host}")
    private String host; //邮件发送者
    @Value("${Spring.mail.port}")
    private String port; //邮件发送者
    @Value("${Spring.mail.rec}")
    private String rec; //邮件发送者
    private final int i = 7; //邮件发送者
    @Resource
    MsgProducer msgProducer;
    @Resource
    private UserInfoDao userDao;
    @Resource
    private TransactionService transactionService;
    @Resource
    private ProgrammeService programmeService;
    @Resource
    private TeamService teamService;
    @Resource
    private RouteInfoService routeInfoService;
    /**
     * 每天零点
     */
    @Scheduled(cron = "0 0 0 * * ?")
    @Async("taskExecutorConfig")
    public void insertRouteInfo() throws Exception {
        List<UserInfo> userInfos = userDao.queryAll(new UserInfo()); // 查询用户
        List<TeamInfo> teamInfoList = teamService.queryAllTeam(new TeamInfo()); // 查询团队
        List<TeamUser> teamUserList = teamService.queryAllUser();//团队成员
        String s = dateTime(initDateByDay());
        List<RouteInfo> routeInfos = routeInfoService.queryAllByDate(s,s);
        List<RouteInfo> routeInfoList = new ArrayList<>();
        teamInfoList.forEach(teamInfo -> {
            List<UserInfo> userInfoList = new ArrayList<>();
            //团队成员
            List<TeamUser> collect = teamUserList.stream().filter(teamUser -> teamInfo.getId().equals(teamUser.getTeamId())).collect(Collectors.toList());
            if (collect.size() > 0) {
                collect.forEach(teamUser -> {
                    List<UserInfo> collect1 = userInfos.stream().filter(userInfo -> userInfo.getId().equals(teamUser.getUserId())).collect(Collectors.toList());
                    if (collect1.size() > 0) {
                        userInfoList.add(collect1.get(0));
                    }
                });
            }
            userInfoList.forEach(userInfo -> {
                List<RouteInfo> collect1 = routeInfos.stream().filter(routeInfo1 -> routeInfo1.getUserId().equals(userInfo.getId())).collect(Collectors.toList());
                if (collect1.size() <= 0) {
                    RouteInfo routeInfo = new RouteInfo();
                    routeInfo.setId(IdUtil.getStringId());
                    routeInfo.setStatus(3);
                    routeInfo.setDateTime(initDateByDay());
                    routeInfo.setUpdateTime(new Date());
                    routeInfo.setAddTime(new Date());
                    routeInfo.setUserId(userInfo.getId());
                    routeInfo.setUsername(userInfo.getUsername());
                    routeInfoList.add(routeInfo);
                }
            });
        });
        if (routeInfoList.size() > 0) {
            routeInfoService.insertBatch(routeInfoList);
        }
    }
    /**
     * 每周五发送邮件（所行程单）
     */
    @Scheduled(cron = "0 0 8 ? * FRI")
    @Async("taskExecutorConfig")
    public void getRouteInfo() throws Exception {
        HashMap map = new HashMap(16);
        map.put("1",dateTime(initDateByDay()));
        map.put("2",dateTime(DateUtil.getAddDate(initDateByDay(), 1)));
        map.put("3",dateTime(DateUtil.getAddDate(initDateByDay(), 2)));
        map.put("4",dateTime(DateUtil.getAddDate(initDateByDay(), 3)));
        map.put("5",dateTime(DateUtil.getAddDate(initDateByDay(), 4)));
        map.put("6",dateTime(DateUtil.getAddDate(initDateByDay(), 5)));
        map.put("7",dateTime(DateUtil.getAddDate(initDateByDay(), 6)));
        map.put("one",dateToWeek(map.get("1").toString()));
        map.put("two",dateToWeek(map.get("2").toString()));
        map.put("three",dateToWeek(map.get("3").toString()));
        map.put("four",dateToWeek(map.get("4").toString()));
        map.put("five",dateToWeek(map.get("5").toString()));
        map.put("six",dateToWeek(map.get("6").toString()));
        map.put("seven",dateToWeek(map.get("7").toString()));

        List<UserInfo> userInfos = userDao.queryAll(new UserInfo()); // 查询用户
        TeamInfo teamInfo1 = new TeamInfo();
        teamInfo1.setStatus(0);
        List<TeamInfo> teamInfoList = teamService.queryAllTeam(teamInfo1); // 查询团队
        List<TeamUser> teamUserList = teamService.queryAllUser();//团队成员
        List<RouteInfo> routeInfos = routeInfoService.queryAllByDate(map.get("1").toString(),map.get("7").toString());
        teamInfoList.forEach(teamInfo -> {
            List<UserInfo> userInfoList = new ArrayList<>();
            //团队成员
            List<TeamUser> collect = teamUserList.stream().filter(teamUser -> teamInfo.getId().equals(teamUser.getTeamId())).collect(Collectors.toList());
            if (collect.size() > 0) {
                collect.forEach(teamUser -> {
                    List<UserInfo> collect1 = userInfos.stream().filter(userInfo -> userInfo.getId().equals(teamUser.getUserId())).collect(Collectors.toList());
                    if (collect1.size() > 0) {
                        userInfoList.add(collect1.get(0));
                    }
                });
            }
            List<MailBean> mailBeanList = new ArrayList<>();
            userInfoList.forEach(userInfo -> {
                if (userInfo.getEMail() != null && !"".equals(userInfo.getEMail())) {
                    List<UserInfo> userInfoList1 = userInfoList.stream().filter(userInfo1 -> !userInfo1.getId().equals(userInfo.getId())).collect(Collectors.toList());
                    List<RouteInfo> collect1 = routeInfos.stream().filter(routeInfo1 -> routeInfo1.getUserId().equals(userInfo.getId())).collect(Collectors.toList());//获得属于用户的行程单
                    List<RouteInfo> collect2 = routeInfos.stream().filter(routeInfo1 -> !routeInfo1.getUserId().equals(userInfo.getId())).collect(Collectors.toList());//获得不属于用户的行程单
                    collect1 = collect1.stream().sorted(Comparator.comparing(RouteInfo::getDateTime,Comparator.reverseOrder())).collect(Collectors.toList());
                    collect2 = collect2.stream().sorted(Comparator.comparing(RouteInfo::getDateTime,Comparator.reverseOrder())).collect(Collectors.toList());
                    StringBuilder stringBuilder = new StringBuilder();
                    List<RouteInfo> route = getRoute(i - collect1.size(), collect1, userInfo, map);
                    isUser(userInfo,route,collect2, stringBuilder, map,userInfoList1);
                    if (stringBuilder.length() > 0) {
                        MailBean mailBean = new MailBean();
                        mailBean.setSubject("["+teamInfo.getTeamName()+"]团队行程单"+map.get("1").toString().replace("-","/")+"~"+map.get("7").toString().replace("-","/"));
                        mailBean.setFromToName(MAIL_SENDER);
                        getTitle(stringBuilder,map);
                        stringBuilder.insert(0,"<h3>账号和密码初始为域账号，例如：账号为w00123456,密码则为w00123456，如果默认的账号和密码不能登录，请espace联系郭闯（wx924941）。<br/>行程数据仅供参考，如有不符，请点击“查看更多日期或更改行程”</h3>");
                        stringBuilder.insert(0,MAIL_HTML);
                        stringBuilder.append(MAIL_HTML_TABLE);
                        stringBuilder.append(MAIL_HTML_BODY);
                        mailBean.setContent(stringBuilder.toString());
                        mailBean.setRecipient(userInfo.getEMail());
                        mailBean.setRecipientName(userInfo.getFullName());
                        mailBeanList.add(mailBean);
                    }
                }
            });
            if (mailBeanList.size() > 0) {
                msgProducer.sendMsg(teamInfo,mailBeanList,0);
            }
        });
    }
    private void getTitle(StringBuilder stringBuilder,HashMap map) {
        stringBuilder.insert(0,THEAD);
        stringBuilder.insert(0,TD_1.concat("操作").concat(TD));
        stringBuilder.insert(0,TD_1.concat(map.get("7").toString()).concat(map.get("seven").toString()).concat(TD));
        stringBuilder.insert(0,TD_1.concat(map.get("6").toString()).concat(map.get("six").toString()).concat(TD));
        stringBuilder.insert(0,TD_1.concat(map.get("5").toString()).concat(map.get("five").toString()).concat(TD));
        stringBuilder.insert(0,TD_1.concat(map.get("4").toString()).concat(map.get("four").toString()).concat(TD));
        stringBuilder.insert(0,TD_1.concat(map.get("3").toString()).concat(map.get("three").toString()).concat(TD));
        stringBuilder.insert(0,TD_1.concat(map.get("2").toString()).concat(map.get("two").toString()).concat(TD));
        stringBuilder.insert(0,TD_1.concat(map.get("1").toString()).concat(map.get("one").toString()).concat(TD));
        stringBuilder.insert(0,TD_1.concat("姓名 工号").concat(TD));
        stringBuilder.insert(0,MAIL_HTML_ROUTE_INFO);
    }

    private StringBuilder isUser(UserInfo userInfo, List<RouteInfo> collect1, List<RouteInfo> collect2, StringBuilder stringBuilder, HashMap map, List<UserInfo> userInfoList1) {
        if (collect1.size() != i) {
            collect1 = getRoute(i - collect1.size(), collect1, userInfo, map);
        }
        appendString(stringBuilder,userInfo,collect1);
        userInfoList1.forEach(userInfo1 -> {
            List<RouteInfo> collect = collect2.stream().filter(routeInfo -> routeInfo.getUserId().equals(userInfo1.getId())).collect(Collectors.toList());
            if (collect.size() <= i) {
                List<RouteInfo> route = getRoute(i - collect.size(), collect, userInfo1, map);
                collect = route;
            }
            appendString(stringBuilder,userInfo1,collect);
        });
        return stringBuilder;
    }

    private List<RouteInfo> getRoute(int i, List<RouteInfo> collect,UserInfo userInfo,HashMap map) {
        List<RouteInfo> collect1 = new ArrayList<>(collect);
        int j = 0;
        if (collect.size() == 0) {
            j = 1;
        }else {
            j = collect.size()+1;
        }
        for ( int y = 0 ;y < i; y++){
            RouteInfo routeInfo = new RouteInfo();
            routeInfo.setId(IdUtil.getStringId());
            routeInfo.setStatus(3);
            if (j<=7) {
                String s = map.get(String.valueOf(j)).toString();
                Date date = dateTime(YYYY_MM_DD, s);
                routeInfo.setDateTime(date);
            }
            routeInfo.setAddress("待定");
            routeInfo.setUpdateTime(new Date());
            routeInfo.setAddTime(new Date());
            routeInfo.setUserId(userInfo.getId());
            routeInfo.setUsername(userInfo.getUsername());
            collect1.add(routeInfo);
            j++;
        }
        List<RouteInfo> collect2 = collect1.stream().sorted(Comparator.comparing(RouteInfo::getDateTime)).collect(Collectors.toList());
        return collect2;
    }
    private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        ConcurrentHashMap<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
    private void appendString(StringBuilder stringBuilder, UserInfo userInfo, List<RouteInfo> collect1) {
        stringBuilder.append("<tr>  \n" +
                "        <td >"+userInfo.getFullName()+"  "+userInfo.getUsername()+"</td>\n");
        stringBuilder.append(
                "        <td >"+parseRouteStatus(collect1.get(0).getAddress(),collect1.get(0).getStatus(),userInfo)+"<br>["+parseRouteStatus(collect1.get(0).getStatus())+"]\n</td>\n");
        stringBuilder.append(
                "        <td >"+parseRouteStatus(collect1.get(1).getAddress(),collect1.get(1).getStatus(),userInfo)+"<br>["+parseRouteStatus(collect1.get(1).getStatus())+"]\n</td>\n");
        stringBuilder.append(
                "        <td >"+parseRouteStatus(collect1.get(2).getAddress(),collect1.get(2).getStatus(),userInfo)+"<br>["+parseRouteStatus(collect1.get(2).getStatus())+"]\n</td>\n");
        stringBuilder.append(
                "        <td >"+parseRouteStatus(collect1.get(3).getAddress(),collect1.get(3).getStatus(),userInfo)+"<br>["+parseRouteStatus(collect1.get(3).getStatus())+"]\n</td>\n");
        stringBuilder.append(
                "        <td >"+parseRouteStatus(collect1.get(4).getAddress(),collect1.get(4).getStatus(),userInfo)+"<br>["+parseRouteStatus(collect1.get(4).getStatus())+"]\n</td>\n");
        stringBuilder.append(
                "        <td >"+parseRouteStatus(collect1.get(5).getAddress(),collect1.get(5).getStatus(),userInfo)+"<br>["+parseRouteStatus(collect1.get(5).getStatus())+"]\n</td>\n");
        stringBuilder.append(
                "        <td >"+parseRouteStatus(collect1.get(6).getAddress(),collect1.get(6).getStatus(),userInfo)+"<br>["+parseRouteStatus(collect1.get(6).getStatus())+"]\n</td>\n");
        stringBuilder.append(
                "        <td >"+LINK_ROUTE+"</td>\n</tr>\n");
    }

    public String parseRouteStatus(Integer status)
    {
        switch (status)
        {
            case 0:
                return "常驻地";
            case 1:
                return "出差";
            case 2:
                return "出差在途";
            case 3:
                return "待定";
            default:
                return "常驻地";
        }
    }
    public String parseRouteStatus(String status,Integer status1, UserInfo userInfo)
    {
        switch (status1)
        {
            case 0:
                return userInfo.getDefaultAddress();
            case 3:
                return "待定";
            default:
                return status;
        }
    }
    /**
     * 每周四发送邮件（项目和事务的问题，根据团队发送）
     * @throws Exception
     */
    @Scheduled(cron = "0 0 8 ? * THU")
    @Async("taskExecutorConfig")
    public void getProgrammeInfoAndTransactionInfo() throws Exception {
        List<UserInfo> userInfos = userDao.queryAll(new UserInfo()); // 查询用户
        List<TeamInfo> teamInfoList = teamService.queryAllTeam(new TeamInfo()); // 查询团队
        List<TeamUser> teamUserList = teamService.queryAllUser();//团队成员
        ProgrammeInfo programmeInfo = new ProgrammeInfo();
        List<ProgrammeInfo> programmeInfoList = programmeService.queryAllByDate(programmeInfo);// 获取本周的项目
        TransactionInfo transactionInfo = new TransactionInfo();
        List<TransactionInfo> transactionInfoList = transactionService.queryAllByDate(transactionInfo); //获取本周的事务
        teamInfoList.forEach(teamInfo -> {
            // 项目
            List<ProgrammeInfo> programmeInfoList1 = programmeInfoList.stream().filter(programmeInfo1 -> programmeInfo1.getTeamId().equals(teamInfo.getId()) && programmeInfo1.getStatus() != 4).collect(Collectors.toList());
            //事务
            List<TransactionInfo> transactionInfoList1 = transactionInfoList.stream().filter(transactionInfo1 -> transactionInfo1.getTeamId().equals(teamInfo.getId())&& transactionInfo1.getStatus() != 4).collect(Collectors.toList());
            List<UserInfo> userInfoList = new ArrayList<>();
            //团队成员
            List<TeamUser> collect = teamUserList.stream().filter(teamUser -> teamInfo.getId().equals(teamUser.getTeamId())).collect(Collectors.toList());
            if (collect.size() > 0) {
                collect.forEach(teamUser -> {
                    List<UserInfo> collect1 = userInfos.stream().filter(userInfo -> userInfo.getId().equals(teamUser.getUserId())).collect(Collectors.toList());
                    if (collect1.size() > 0) {
                        userInfoList.add(collect1.get(0));
                    }
                });
            }
            List<MailBean> mailBeanList = new ArrayList<>();
            if (userInfoList.size() > 0) {
                userInfoList.forEach(userInfo -> {
                    if (userInfo.getEMail() != null && !"".equals(userInfo.getEMail())) {
                        StringBuilder stringBuilder = new StringBuilder();
                        setContent(programmeInfoList1,userInfo,stringBuilder,teamInfo,userInfoList);
                        if (stringBuilder.length() > 0) {
                            stringBuilder.append(MAIL_HTML_TABLE);
                        }
                        seTransactionContent(transactionInfoList1,userInfo,stringBuilder,teamInfo,userInfoList);
                        if (stringBuilder.length() > 0) {
                            MailBean mailBean = new MailBean();
                            mailBean.setSubject("【"+teamInfo.getTeamName()+"】"+SUBJECT_INFO+"【"+dateTime(new Date())+"】");
                            mailBean.setFromToName(MAIL_SENDER);
                            stringBuilder.insert(0,"<h3>账号和密码初始为域账号，例如：账号为w00123456,密码则为w00123456，如果默认的账号和密码不能登录，请espace联系郭闯（wx924941）</h3>");
                            stringBuilder.insert(0,MAIL_HTML);
                            stringBuilder.append(MAIL_HTML_BODY);
                            mailBean.setContent(stringBuilder.toString());
                            mailBean.setRecipient(userInfo.getEMail());
                            mailBean.setRecipientName(userInfo.getFullName());
                            mailBeanList.add(mailBean);
                        }
                    }
                });
            }
            if (mailBeanList.size() > 0) {
                msgProducer.sendMsg(teamInfo,mailBeanList,1);
            }
        });
    }

    private StringBuilder seTransactionContent(List<TransactionInfo> transactionInfoList1, UserInfo userInfo, StringBuilder stringBuilder, TeamInfo teamInfo, List<UserInfo> userInfoList) {
        List<TransactionInfo> collect1 = transactionInfoList1.stream().filter(programmeInfo1 -> programmeInfo1.getUserId().equals(userInfo.getId()) &&programmeInfo1.getTeamId().equals(teamInfo.getId())).collect(Collectors.toList());
        if (collect1.size() > 0) {
            stringBuilder.append(MAIL_HTML_TRANSACTION_INFO);
            collect1.forEach(programmeInfo1 -> {
                appendTransactionContentString(stringBuilder,programmeInfo1,userInfo);
            });
            collect1 = transactionInfoList1.stream().filter(programmeInfo1 -> !programmeInfo1.getUserId().equals(userInfo.getId()) &&
                    "1".equals(programmeInfo1.getTransactionStatus()) && programmeInfo1.getTeamId().equals(teamInfo.getId())).collect(Collectors.toList());
            if (collect1.size() >0) {
                //公开的
                collect1.forEach(programmeInfo1 -> {
                    List<UserInfo> collect2 = userInfoList.stream().filter(userInfo1 -> userInfo1.getId().equals(programmeInfo1.getUserId())).collect(Collectors.toList());
                    if (collect2.size() > 0) {
                        appendTransactionContentString(stringBuilder,collect2,programmeInfo1,userInfo);
                    }
                });
            }
        }else {
            collect1 = transactionInfoList1.stream().filter(programmeInfo1 -> !programmeInfo1.getUserId().equals(userInfo.getId()) &&
                    "1".equals(programmeInfo1.getTransactionStatus())&& programmeInfo1.getTeamId().equals(teamInfo.getId())).collect(Collectors.toList());
            if (collect1.size() >0) {
                stringBuilder.append(MAIL_HTML_TRANSACTION_INFO);
                //公开的
                collect1.forEach(programmeInfo1 -> {
                    List<UserInfo> collect2 = userInfoList.stream().filter(userInfo1 -> userInfo1.getId().equals(programmeInfo1.getUserId())).collect(Collectors.toList());
                    if (collect2.size() > 0) {
                        appendTransactionContentString(stringBuilder,collect2,programmeInfo1,userInfo);
                    }
                });
            }
        }
        return stringBuilder;
    }

    /**
     * 事务
     * @param stringBuilder
     * @param collect2
     * @param programmeInfo1
     * @param userInfo
     */
    private void appendTransactionContentString(StringBuilder stringBuilder, List<UserInfo> collect2, TransactionInfo programmeInfo1, UserInfo userInfo) {
        stringBuilder.append("<tr>  \n" +
                "        <td >" + programmeInfo1.getNo() + "</td>\n" +
                "        <td >" + programmeInfo1.getDesc() + "</td>\n" +
                "        <td >" + collect2.get(0).getUsername().concat(collect2.get(0).getFullName()) + "</td>\n" +
                "        <td >" + DateUtil.datetoString(programmeInfo1.getTransactionTime()) + "</td>\n" +
                "        <td >" + programmeInfo1.getProgrammeName() + "</td>\n");
        appendString(stringBuilder, programmeInfo1);
    }

    /**
     * 事务
     * @param stringBuilder
     * @param transactionInfo
     * @param userInfo
     */
    private void appendTransactionContentString(StringBuilder stringBuilder, TransactionInfo transactionInfo, UserInfo userInfo) {
        stringBuilder.append("<tr>  \n" +
                "        <td >"+transactionInfo.getNo()+"</td>\n" +
                "        <td >"+transactionInfo.getDesc()+"</td>\n" +
                "        <td >"+userInfo.getUsername().concat(userInfo.getFullName())+"</td>\n" +
                "        <td >"+ DateUtil.datetoString(transactionInfo.getTransactionTime()) +"</td>\n" +
                "        <td >"+transactionInfo.getProgrammeName()+"</td>\n");
        appendString(stringBuilder,transactionInfo);
    }

    private void appendString(StringBuilder stringBuilder, TransactionInfo transactionInfo) {
        if (transactionInfo.getStatus() == 0) {
            stringBuilder.append("        <td >发现</td>\n");
        }
        if (transactionInfo.getStatus() == 1) {
            stringBuilder.append("        <td >定位</td>\n");
        }
        if (transactionInfo.getStatus() == 2) {
            stringBuilder.append("        <td >现网带缺陷闭环</td>\n");
        }
        if (transactionInfo.getStatus() == 3) {
            stringBuilder.append("        <td >挂起</td>\n");
        }
        if (transactionInfo.getStatus() == 4) {
            stringBuilder.append("        <td >正式闭环</td>\n");
        }
        stringBuilder.append(
                "        <td >"+LINK+"</td>\n" +
                        "      </tr>\n");
    }

    private StringBuilder setContent(List<ProgrammeInfo> programmeInfoList1, UserInfo userInfo, StringBuilder stringBuilder,TeamInfo teamInfo,List<UserInfo> userInfoList ) {
        List<ProgrammeInfo> collect1 = programmeInfoList1.stream().filter(programmeInfo1 -> programmeInfo1.getUserId().equals(userInfo.getId())&& programmeInfo1.getTeamId().equals(teamInfo.getId())).collect(Collectors.toList());
        if (collect1.size() > 0) {
            stringBuilder.append(MAIL_HTML_PROGRAMME_INFO);
            collect1.forEach(programmeInfo1 -> {
                appendProgrammeString1(userInfo,stringBuilder,programmeInfo1,teamInfo);
            });
            collect1 = programmeInfoList1.stream().filter(programmeInfo1 -> !programmeInfo1.getUserId().equals(userInfo.getId()) &&
                    "1".equals(programmeInfo1.getProgrammeStatus())&& programmeInfo1.getTeamId().equals(teamInfo.getId())).collect(Collectors.toList());
            //公开的
            collect1.forEach(programmeInfo1 -> {
                List<UserInfo> collect2 = userInfoList.stream().filter(userInfo1 -> userInfo1.getId().equals(programmeInfo1.getUserId())).collect(Collectors.toList());
                if (collect2.size() > 0) {
                    appendProgrammeString(collect2,stringBuilder,programmeInfo1,teamInfo);
                }
            });
        }else {
            collect1 = programmeInfoList1.stream().filter(programmeInfo1 -> !programmeInfo1.getUserId().equals(userInfo.getId()) &&
                    "1".equals(programmeInfo1.getProgrammeStatus())&& programmeInfo1.getTeamId().equals(teamInfo.getId())).collect(Collectors.toList());
            if (collect1.size() > 0) {
                stringBuilder.append(MAIL_HTML_PROGRAMME_INFO);
                //公开的
                collect1.forEach(programmeInfo1 -> {
                    List<UserInfo> collect2 = userInfoList.stream().filter(userInfo1 -> userInfo1.getId().equals(programmeInfo1.getUserId())).collect(Collectors.toList());
                    if (collect2.size() > 0) {
                        appendProgrammeString(collect2,stringBuilder,programmeInfo1,teamInfo);
                    }
                });
            }
        }
        return stringBuilder;
    }

    /**
     * 项目
     * status 0--启动 1--POC试用 2--形成PO 3--交付验收 4--形成收入 1-1--挂起（11试用未转商用）1-2--丢标（12异常关闭）
     * @param userInfo
     * @param stringBuilder
     * @param programmeInfo1
     * @param teamInfo
     */
    private void appendProgrammeString1(UserInfo userInfo, StringBuilder stringBuilder, ProgrammeInfo programmeInfo1, TeamInfo teamInfo) {
        stringBuilder.append("<tr>  \n" +
                "        <td >"+programmeInfo1.getNum()+"</td>\n" +
                "        <td >"+programmeInfo1.getName()+"</td>\n" +
                "        <td >"+userInfo.getUsername().concat(userInfo.getFullName())+"</td>\n" +
                "        <td >"+DateUtil.datetoString(programmeInfo1.getAddTime())+"</td>\n");
        appendProgrammeString(stringBuilder, programmeInfo1, teamInfo);
    }

    private void appendProgrammeString(StringBuilder stringBuilder, ProgrammeInfo programmeInfo1, TeamInfo teamInfo) {
        if (programmeInfo1.getStatus() == 0) {
            stringBuilder.append("        <td >启动</td>\n");
        }
        if (programmeInfo1.getStatus() == 1) {
            stringBuilder.append("        <td >POC试用</td>\n");
        }
        if (programmeInfo1.getStatus() == 2) {
            stringBuilder.append("        <td >形成PO</td>\n");
        }
        if (programmeInfo1.getStatus() == 3) {
            stringBuilder.append("        <td >交付验收</td>\n");
        }
        if (programmeInfo1.getStatus() == 4) {
            stringBuilder.append("        <td >形成收入</td>\n");
        }
        if (programmeInfo1.getStatus() == 5) {
            stringBuilder.append("        <td >持续运维</td>\n");
        }
        if (programmeInfo1.getStatus() == 11) {
            stringBuilder.append("        <td >挂起</td>\n");
        }
        if (programmeInfo1.getStatus() == 12) {
            stringBuilder.append("        <td >丢标</td>\n");
        }
        stringBuilder.append("        <td >").append(teamInfo.getTeamName()).append("</td>\n").append("        <td >").append(LINK).append("</td>\n").append("      </tr>\n");
    }

    /**
     * 项目
     * @param collect2
     * @param stringBuilder
     * @param programmeInfo1
     * @param teamInfo
     */
    private void appendProgrammeString(List<UserInfo> collect2, StringBuilder stringBuilder, ProgrammeInfo programmeInfo1, TeamInfo teamInfo) {
        stringBuilder.append("<tr>  \n" +
                "        <td >" + programmeInfo1.getNum() + "</td>\n" +
                "        <td >" + programmeInfo1.getName() + "</td>\n" +
                "        <td >" + collect2.get(0).getUsername().concat(collect2.get(0).getFullName()) + "</td>\n" +
                "        <td >" + DateUtil.datetoString(programmeInfo1.getAddTime()) + "</td>\n");
        appendProgrammeString(stringBuilder, programmeInfo1, teamInfo);
    }

}
